//
//  GPS.m
//  itaka
//
//  Created by Robert Soszynski on 1/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GPS.h"

@implementation GPS
@synthesize locationManager, reverseGeocoder, address, latitude, longitude, timestamp, delegate, onDemand;

- (id)init {
    if (self = [super init])
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
        locationManager.headingFilter = kCLHeadingFilterNone;
        
        NSLog(@"Significant Location Change Available: %d", [CLLocationManager significantLocationChangeMonitoringAvailable]);
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    
    //NSTimeInterval locationAge = fabs([oldLocation.timestamp timeIntervalSinceNow]);
    
    [self setLatitude: [NSNumber numberWithFloat:newLocation.coordinate.latitude]];
    [self setLongitude: [NSNumber numberWithFloat:newLocation.coordinate.longitude]];
    [self setTimestamp: newLocation.timestamp];
    
    /*CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];
    [reverseGeocoder reverseGeocodeLocation: locationManager.location completionHandler: ^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        NSLog(@"placemark %@", placemark);
    }];*/
    
    reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
    
}

- (void)start {
    [locationManager startMonitoringSignificantLocationChanges];
    
    NSLog(@"GPS: On");
}

- (void)stop {
    [locationManager stopMonitoringSignificantLocationChanges];
    
    NSLog(@"GPS: Off");
}

- (void)startOnDemand {
    [self setOnDemand:TRUE];
    [locationManager stopMonitoringSignificantLocationChanges];
    
    [locationManager startUpdatingLocation];
    
    NSLog(@"GPS on demand: On");
}

- (void)stopOnDemand {
    [self setOnDemand:FALSE];
    [locationManager stopUpdatingLocation];
    
    [locationManager stopMonitoringSignificantLocationChanges];
    
    NSLog(@"GPS on demand: Off");
}

// Delegate methods
- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFindPlacemark:(MKPlacemark*)place
{
    NSDictionary *fullAddress = [place addressDictionary];
    NSArray *arrAddress = [fullAddress objectForKey:@"FormattedAddressLines"];
    
    [self setAddress:[NSString stringWithFormat:@"%@,\n%@,\n%@", [arrAddress objectAtIndex:0], [arrAddress objectAtIndex:1], [arrAddress objectAtIndex:2]]];
    
	[geocoder cancel];
    
    [[self delegate] GPSUpdated];
}

- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFailWithError:(NSError*)error
{
    [self setAddress:@"no info"];
    
    [[self delegate] GPSUpdated];
}

@end
